﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameControl : MonoBehaviour
{
    public GameObject levelOverNotic;
    public LevelNotic levelNotic;
    public static GameControl instance;
    public PlayerControl player;
    private Rigidbody2D playerRigi;
    
    public SkinClass curSkin;
    public Vector2 origiPoint;
    public GameObject gameLevel;
    public GameObject mainCamera;

    private GameObject[] points;

    private void Awake()
    {
        if (instance != null)
        {
            Destroy(this);
        }
        instance = this;
        playerRigi = player.GetComponent<Rigidbody2D>();
    }

    bool inGame;
    bool gameWin;
    bool gameFalse;

    /// <summary>
    /// 游戏中
    /// </summary>
    public bool InGame { get => inGame; set => inGame = value; }
    /// <summary>
    /// 游戏胜利
    /// </summary>
    public bool GameWin { get => gameWin; set => gameWin = value; }
    /// <summary>
    /// 游戏失败
    /// </summary>
    public bool GameFalse { get => gameFalse; set => gameFalse = value; }
    
    /// <summary>
    /// 游戏开始
    /// </summary>
    public void GameBegin()
    {
        if (User.instance.CurLevel >= User.instance.MaxLevel)
        {
            //Debug.LogError("更多内容\n敬请期待");
            StopAllCoroutines();
            StartCoroutine(NoticShow());
            return;
        }

        InGame = true;

        player.spriteRenderer.sprite = Resources.Load<Sprite>(curSkin.skinName + "/IdleImage");
        player.idleSprite = Resources.Load<Sprite>(curSkin.skinName + "/IdleImage");
        player.tileSprite = Resources.Load<Sprite>(curSkin.skinName + "/TiltImage");
        player.victorySprite = Resources.Load<Sprite>(curSkin.skinName + "/WinImage");
        　
        //player.spriteRenderer.sprite = curSkin.idleImage;
        //player.idleSprite = curSkin.idleImage;
        //player.tileSprite = curSkin.tiltImage;
        //player.victorySprite = curSkin.winImage;
        player.gameObject.SetActive(true);

        //gameLevel = Instantiate(Resources.Load<GameObject>("levelPrefab/level " + 25));
        gameLevel = Instantiate(Resources.Load<GameObject>("levelPrefab/level " + User.instance.CurLevel));

        levelNotic.ending = gameLevel.transform.Find("Ending").gameObject;
        levelNotic.gameObject.SetActive(true);

        UIManager.Instance.PushPanel(UIPanelType.Panel_InGame);

        points = GameObject.FindGameObjectsWithTag("Point");
        player.GetComponent<PlayerControl>().points = points;


    }

    /// <summary>
    /// 角色掉出范围后失败
    /// </summary>
    /// <param name="collision"></param>
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (gameWin)
        {
            return;
        }

        if (collision.tag.Equals("Player"))
        {
            GameFalseAction();
            
        }
    }

    /// <summary>
    /// 游戏胜利后的动作
    /// </summary>
    public IEnumerator GameWinAction()
    {
        GameControl.instance.InGame = false;
        GameControl.instance.GameWin = true;
        //Debug.Log("胜利");
        player.OnMouseUp();

        player.spriteRenderer.sprite = player.victorySprite;
        player.transform.up = playerRigi.velocity;
        playerRigi.gravityScale = 0;
        playerRigi.constraints = RigidbodyConstraints2D.FreezeRotation;

        Time.timeScale = 0.8f;
        //playerRigi.velocity *= 0.25f;
        playerRigi.velocity = playerRigi.velocity.normalized * 2f;

        yield return new WaitForSeconds(1f);

        User.instance.ThroughLevel();
        GameOver();
        UIManager.Instance.PopPanel();
        UIManager.Instance.PushPanel(UIPanelType.Panel_win);
    }

    /// <summary>
    /// 游戏失败后的动作
    /// </summary>
    public void GameFalseAction()
    {
        //Debug.Log(origiPoint);
        InGame = false;
        GameFalse = true;

        GameOver();
        UIManager.Instance.PopPanel();
        UIManager.Instance.PushPanel(UIPanelType.Panel_False);
    }

    /// <summary>
    /// 游戏结束，角色回归原位
    /// </summary>
    public void GameOver()
    {
        inGame = false;
        gameFalse = false;
        GameWin = false;
        player.OnMouseUp();

        player.gameObject.SetActive(false);
        player.gameObject.transform.position = origiPoint;
        playerRigi.gravityScale = 1.5f;
        playerRigi.constraints = RigidbodyConstraints2D.None;

        mainCamera.transform.position = new Vector3(0, 0, -10);

        levelNotic.gameObject.SetActive(false);
        
        Destroy(gameLevel.gameObject);
        points = null;
        player.GetComponent<PlayerControl>().points = points;
    }

    /// <summary>
    /// 关卡通关提示
    /// </summary>
    /// <returns></returns>
    public IEnumerator NoticShow()
    {
        levelOverNotic.gameObject.SetActive(true);

        yield return new WaitForSeconds(1f);
        levelOverNotic.gameObject.SetActive(false);
    }





}
